import 'package:syria_sports/core/constants/pref_keys.dart';
import 'package:flutter/foundation.dart';
import 'package:shared_preferences/shared_preferences.dart';

import '../../data/models/auth/user.dart';
import '../../presentation/admin_home_tabs/cp_tab/models/home_model.dart';

class PrefUtils {
  static late SharedPreferences _sharedPreferences;

  static Future init() async {
    _sharedPreferences = await SharedPreferences.getInstance();
    debugPrint('SharedPreference Initialized');
  }

  ///will clear all the data stored in preference
  static void clearPreferencesData() async {
    _sharedPreferences.clear();
  }

  static Future<void> setThemeData(String value) {
    return _sharedPreferences.setString(PrefKeys.themeData, value);
  }

  static String getThemeData() {
    try{
      return _sharedPreferences.getString(PrefKeys.themeData)??'dark';
    }catch (e) {
      debugPrint( 'Error in PrefUtils on getThemeData Function :\n$e');
      return 'dark';
    }
  }
  static bool isLogged() {
    try {
      return _sharedPreferences.getString(PrefKeys.token)!= null;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }

  static bool isAdmin() {
    try {
      return _sharedPreferences.getString(PrefKeys.userType) == '3';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static bool isGymAdmin() {
    try {
      return _sharedPreferences.getString(PrefKeys.userType) == '4' || _sharedPreferences.getString(PrefKeys.userType) == '3' ;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }

  static bool isCoach() {
    try {
      return _sharedPreferences.getString(PrefKeys.userType) == '2' || _sharedPreferences.getString(PrefKeys.userType) == '3' || _sharedPreferences.getString(PrefKeys.userType) == '4';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static String getUserId() {
    try {
      return _sharedPreferences.getString(PrefKeys.userId)??'';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }

  static String getFullName() {
    try {
      return _sharedPreferences.getString(PrefKeys.userFullName)??'';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }

  static String getBarcode() {
    try {
      return _sharedPreferences.getString(PrefKeys.barcode)??'';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }
  static bool isQRQuickAccessEnabled() {
    try {
      //ميزة الوصول السريع الى ال qr تكون مفعلة بشكل افتراضي
      return _sharedPreferences.getBool(PrefKeys.isQRQuickAccessEnabled)??true;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static void changeDarkMode() {
    try {
      _sharedPreferences.setBool(PrefKeys.isDarkMode, !isDarkMode());
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
    }
  }

  static bool isDarkMode() {
    try {
      //ميزة الوضع المظلم تكون مفعلة بشكل افتراضي
      return _sharedPreferences.getBool(PrefKeys.isDarkMode)??true;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static void changeQRQuickAccessEnabled() {
    try {
      _sharedPreferences.setBool(PrefKeys.isQRQuickAccessEnabled, !isQRQuickAccessEnabled());
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
    }
  }
  static String? getImageLink() {
    try {
      String? res = _sharedPreferences.getString(PrefKeys.userImage);
      if(res.toString().isEmpty){
        res = null;
      }

      return res;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }

  static String getToken() {
    try {
      return _sharedPreferences.getString(PrefKeys.token)??'';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }

  static bool myPrivacy() {
    try {
      return _sharedPreferences.getBool(PrefKeys.publicInfo)??false;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static bool changeMyPrivacy() {
    try {
      var privacy = _sharedPreferences.getBool(PrefKeys.publicInfo)??false;
       _sharedPreferences.setBool(PrefKeys.publicInfo, !privacy );
      return !privacy;
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return false;
    }
  }
  static String getAdminsSubscriptionsId() {
    try {
      return _sharedPreferences.getString(PrefKeys.adminsSubscriptionsId)??'';
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
      return '';
    }
  }

  static void saveHomeData(String homeData) {
    try {
      _sharedPreferences.setString(PrefKeys.homeData,homeData);
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
    }
  }
  static HomeModel? getHomeData() {
    try {
      var res =_sharedPreferences.getString(PrefKeys.homeData);
      if(res != null && res!= '') {
        return HomeModel.fromRawJson(res);
      }
    } catch (e) {
      debugPrint( 'error in PrefUtils on isLogged Function :\n$e');
    }
    return null;
  }

  static Future<void> saveUserData(User value) {
    _sharedPreferences.setString(PrefKeys.token, value.token??'');
    _sharedPreferences.setString(PrefKeys.userImage, value.image??'');
    _sharedPreferences.setString(PrefKeys.userType, value.type);
    _sharedPreferences.setString(PrefKeys.userName, value.username??'');
    _sharedPreferences.setString(PrefKeys.barcode, value.barcode??'');
    _sharedPreferences.setString(PrefKeys.userFullName, value.fullName);
    _sharedPreferences.setString(PrefKeys.adminsSubscriptionsId, value.adminsSubscriptionsId??'');
    _sharedPreferences.setBool(PrefKeys.publicInfo, value.public??false);
    return _sharedPreferences.setString(PrefKeys.userId, value.userId??"");
  }


}
